package com.apps.ftapp.application;

import android.content.Context;
import android.graphics.Typeface;

import androidx.annotation.NonNull;
import androidx.multidex.MultiDex;

import com.apps.ftapp.R;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.SharedPreferencesUtil;
import com.duobeiyun.common.DBYHelper;
import com.liulishuo.filedownloader.FileDownloader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

import org.litepal.LitePal;
import org.litepal.LitePalApplication;

import java.lang.reflect.Field;

import uk.co.chrisjenx.calligraphy.CalligraphyConfig;

/**
 * Created by lyli on 2017/10/2.
 */

public class App extends LitePalApplication {

    private static final String TAG = "JIGUANGTAG";

    static {
        //设置全局的Header构建器

        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @NonNull
            @Override
            public RefreshFooter createRefreshFooter(@NonNull Context context, @NonNull RefreshLayout layout) {
                return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate);
            }
        });


        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @NonNull
            @Override
            public RefreshHeader createRefreshHeader(@NonNull Context context, @NonNull RefreshLayout layout) {
                layout.setPrimaryColorsId(R.color.white, R.color.color_title_color);//全局设置主题颜色
                ClassicsHeader header = new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);
                header.setTextSizeTitle(13);//设置标题文字大小（sp单位）
                //     header.setTextSizeTitle(13, TypedValue.COMPLEX_UNIT_SP);//同上
                return header; //指定为经典Header，默认是 贝塞尔雷达Header
            }
        });

    }


    @Override
    public void onCreate() {
        super.onCreate();
        LitePal.initialize(this);
        CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                .setDefaultFontPath("fonts/dq.ttf")
                .setFontAttrId(R.attr.fontPath)
                .build()
        );
        //极光配置
//        JPushInterface.setDebugMode(true);
//        JPushInterface.init(this);

        DBYHelper.getInstance().initDBY(this);
        //读取缓存信息
        if (!Constant.IS_DEBUG) {
            Constant.uid = SharedPreferencesUtil.getData(getApplicationContext(), "uid", "") + "";
            Constant.token = SharedPreferencesUtil.getData(getApplicationContext(), "token", "") + "";
            Constant.nickname = SharedPreferencesUtil.getData(getApplicationContext(), "nickname", "") + "";//result.getInfo().getUser().getNickname();
        }
        FileDownloader.setup(this);

//        Hawk.init(this);
    }


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    private void setDefaultFont() {
        try {
            final Typeface bold = Typeface.createFromAsset(getAssets(),
                    "Hiragino Sans GB W6.otf");
            final Typeface italic = Typeface.createFromAsset(getAssets(),
                    "dq.ttf");
            final Typeface boldItalic = Typeface.createFromAsset(getAssets(),
                    "Hiragino Sans GB W6.otf");
            final Typeface regular = Typeface.createFromAsset(getAssets(),
                    "dq.ttf");

            Field DEFAULT = Typeface.class.getDeclaredField("DEFAULT");
            DEFAULT.setAccessible(true);
            DEFAULT.set(null, regular);

            Field DEFAULT_BOLD = Typeface.class
                    .getDeclaredField("DEFAULT_BOLD");
            DEFAULT_BOLD.setAccessible(true);
            DEFAULT_BOLD.set(null, bold);

            Field sDefaults = Typeface.class.getDeclaredField("sDefaults");
            sDefaults.setAccessible(true);
            sDefaults.set(null, new Typeface[]{regular, bold, italic,
                    boldItalic});

        } catch (Throwable e) {
            e.printStackTrace();
        }
    }


}
